// pages/doctor-goods-add-3/doctor-goods-add-3.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgBase: app.globalData.imgUrl,          // 线上图片
    localImg: app.globalData.staticimgUrl,  // 本地图片
    productDesc: '', // 产品描述
    productPic: '', // 医美产品图片
    images: [],
    imagesurl: [], // 上传成功的图片地址
    options: '',
    ajaxLoading: false,
    imgFile:[],
    imagesArr:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var curr = getCurrentPages();
    this.setData({
      'options': options,
    })
  },

  // 添加补充信息
  addProductDesc: function(e) {
    var productDesc = e.detail.value;
    this.setData({
      'productDesc': productDesc,
    })
    console.log(this.data.productDesc);

  },

  // 上传图片
  chooseImg: function(e) {
    var that = this;
    var imgLength = this.data.images.length;
    wx.chooseImage({
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      count: 5 - imgLength,
      success: function(res) {
       
        // 将现有和新上传的合并
        var images = that.data.images.concat(res.tempFilePaths); // 合并原有数组和现传照片
       
        that.setData({
          images: images,
          imgFile: res.tempFilePaths
        })
        that.uploadimages(); // 选择图片后 上传到服务器上
      },
    });
  },

  // 上传图片
  uploadimages: function(e) {
    var that = this;
    let { imagesurl, imgFile} = this.data;
    const images = imgFile;
  
    wx.showToast({
      icon: "loading",
      title: "正在上传"
    });

   
    for (var i = 0; i < images.length; i++) {
      var imagesArr = [];
      app.uploadFile({
        // url: 'doctor/product/uploadFile',
      
        url:'doctor/docExample/uploadFile',
        filePath: images[i],
        success: function(res) {
          var path = JSON.parse(res.data).data[0].filePath;
         
          imagesArr.push(path);
          that.setData({
            imagesurl: imagesurl.concat(imagesArr)
          })
        }
      })
    };

  },



  //  预览图片
  handleImagePreview: function(e) {
    var index = e.target.dataset.index;
    var images = this.data.images;
    wx.previewImage({
      current: images[index],
      urls: images,
    })
  },

  // 删除图片
  handleDeleteImage: function(e) {
    var index = e.target.dataset.index;
    var imagesurl = this.data.imagesurl;
    var images = this.data.images;
    imagesurl.splice(index, 1);
    images.splice(index, 1);
    this.setData({
      'imagesurl': imagesurl,
      'images': images,
    });
    console.log(this.data.imagesurl);
  },

  // 完成商品添加
  completeAddGoods: function(e) {
    let{ ajaxLoading } = this.data;
    if (!this.data.productDesc) {
      wx.showToast({
        title: '请添加补充信息',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (this.data.images.length == 0) {
      wx.showToast({
        title: '请添加图片',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if(!ajaxLoading){
      this.setData({
        ajaxLoading: true
      });
      this.handleSubmit();
    } 
  },

  handleSubmit: function(e) {
    var that = this;
    var oneType = this.data.options.choosedid.split(",")[0];
    var twoType = this.data.options.choosedid.split(",")[1];
    var threeType = this.data.options.choosedid.split(",")[2];
    var productpic = this.data.options.productPic;
    var productname = this.data.options.productName;
    var serviceprice = this.data.options.servicePrice;
    var totalprice = this.data.options.totalPrice;
    var makeprice = this.data.options.makePrice;
    var cityType = this.data.options.cityType;
    var deviceId = this.data.options.deviceId;
    var narcosis = this.data.options.narcosis;
    app.request({
      url: 'doctor/product/create',
      data: {
        'oneType': oneType,
        'twoType': twoType,
        'threeType': threeType,
        'productPic': productpic,
        'productName': productname,
        'cityType': cityType,
        'deviceId': deviceId,
        'narcosis': narcosis,
        'servicePrice': serviceprice,
        'totalPrice': totalprice,
        'makePrice': makeprice,
        'contentPic': that.data.imagesurl,
        'productDesc': that.data.productDesc,
      },
      success(res) {
        that.setData({
          ajaxLoading: false
        });
        wx.redirectTo({
          url: '../doctor-case-add-5/doctor-case-add-5?type=2',
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})